package com.v8dn.app.common.web;

import java.util.Locale;
import java.util.Map;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

/**
 * @title 多视图处理器
 * @author lhf
 * @date 2012-11-3 下午6:41:07
 * @description
 */
public class MixedViewResolver implements ViewResolver{

	private Map<String,ViewResolver> resolvers;

	public void setResolvers(Map<String,ViewResolver> resolvers){
		this.resolvers=resolvers;
	}

	@Override
	public View resolveViewName(String viewName,Locale locale) throws Exception{
		int n=viewName.lastIndexOf(".");
		if(n!=-1){
			// 取出扩展名
			String suffix=viewName.substring(n+1);
			// 取出对应的ViewResolver
			ViewResolver resolver=resolvers.get(suffix);
			if(resolver==null){
				throw new RuntimeException("No ViewResolver for "+suffix);
			}
			return resolver.resolveViewName(viewName,locale);
		}else{
			ViewResolver resolver=resolvers.get("jsp");
			return resolver.resolveViewName(viewName,locale);
		}
	}

}
